package com.educal.Controller;

import com.educal.Bean.Teacher;
import com.educal.Repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by miyang on 2017/12/17.
 */
@RestController
public class TeacherController {
    @Autowired
    private TeacherRepository teacherRepository;

    /*
    * 增加老师
    * */
    @PostMapping(value = "/teachers")
    public Teacher addTeacher(@RequestParam("tId") String tId,
                          @RequestParam("tPassword") String tPssword,
                          @RequestParam("tName") String tName,
                          @RequestParam("tCollege") String tCollege,
                          @RequestParam("phone") String phone,
                          @RequestParam("position") String position){
        Teacher teacher = new Teacher();

        teacher.setTid(tId);
        teacher.setTpassword(tPssword);
        teacher.setTname(tName);
        teacher.setTcollege(tCollege);
        teacher.setPhone(phone);
        teacher.setPosition(position);

        //System.out.print(stu.getStuid()+ stu.getStuname());

        return teacherRepository.save(teacher);
    }

    /*
    * 查询全部学生信息*/
    @GetMapping(value= "/teachers")
    public List<Teacher> findAll() {

        return teacherRepository.findAll();
    }

    /*
    * 用id查询*/
    @GetMapping(value = "/teachers/{tId}")
    public Teacher findByTeacherId(@PathVariable("tId") String tId) {
        Teacher teacher = new Teacher();
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaId = list.get(i).getTid();
            if(teaId.equals(tId)){
                teacher.setTid(tId);
                teacher.setTpassword(list.get(i).getTpassword());
                teacher.setTname(list.get(i).getTname());
                teacher.setTcollege(list.get(i).getTcollege());
                teacher.setPhone(list.get(i).getPhone());
                teacher.setPosition(list.get(i).getPosition());
                break;
            }
        }
        return teacher;
    }
    /*
    * 用name查询(报错！！！！！)*/
    /*@PostMapping(value = "/stu/find/name")
        public List<Student> findByName(@RequestParam("stuName") String stuName) {
        List<Student> stuList = null;
        int k = 0;
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sName = list.get(i).getStuname();
            if(sName.equals(stuName)){
                stuList.get(k).setStuid(list.get(i).getStuid());
                System.out.print(list.get(i).getStuid());
                stuList.get(k).setStuname(sName);
                stuList.get(k).setStucollege(list.get(i).getStucollege());
                stuList.get(k).setStuclass(list.get(i).getStuclass());
                k++;
            }
        }
        return stuList;
    }*/


    /*
    * 修改学生信息*/
    @PutMapping(value = "/teachers")
    public Teacher updateTeacherId(@RequestParam("tId") String tId,
                                   @RequestParam("tPassword") String tPssword,
                                   @RequestParam("tName") String tName,
                                   @RequestParam("tCollege") String tCollege,
                                   @RequestParam("phone") String phone,
                                   @RequestParam("position") String position){
        Teacher teacher = new Teacher();

        teacher.setTid(tId);
        teacher.setTpassword(tPssword);
        teacher.setTname(tName);
        teacher.setTcollege(tCollege);
        teacher.setPhone(phone);
        teacher.setPosition(position);

        //System.out.print(stu.getStuid()+ stu.getStuname());

        return teacherRepository.save(teacher);
    }

    /*
    * 用id删除学生*/
    @DeleteMapping(value = "/teachers/{tId}")
    public void deleteTeacher(@PathVariable("tId") String tId){
        Teacher teacher = new Teacher();
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaId = list.get(i).getTid();
            if(teaId.equals(tId)){
                teacher.setTid(tId);
                teacher.setTpassword(list.get(i).getTpassword());
                teacher.setTname(list.get(i).getTname());
                teacher.setTcollege(list.get(i).getTcollege());
                teacher.setPhone(list.get(i).getPhone());
                teacher.setPosition(list.get(i).getPosition());
                teacherRepository.delete(teacher);
                break;
            }
        }
    }
}
